package timer;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by cuihao on 2017/9/6.
 */
public class Task2 {
    public static void main(String[] args) {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {

                System.out.println("第一个定时任务");
            }
        };
        TimerTask timerTask1 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("第二个定时任务");
            }
        };
        Timer timer = new Timer();
        long delay = 0;
        long intevalPetiod = 5;
        timer.scheduleAtFixedRate(timerTask, delay, intevalPetiod);
        timer.scheduleAtFixedRate(timerTask1, delay, intevalPetiod);


        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(2);

        /*
         * 线程
         * 延迟
         * 时间大小
         * 时间单位
         *
         * */
        scheduledExecutorService.scheduleAtFixedRate(timerTask, delay, intevalPetiod, TimeUnit.SECONDS);
        scheduledExecutorService.scheduleAtFixedRate(timerTask1, delay, intevalPetiod, TimeUnit.SECONDS);


    }
}

//scheduledExecutorService可以保证多个任务中任意一个终断时其他的继续，Timer其中一个中断其他也会中断